package com.atguigu.msmservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.msmservice.service.MsmService;
import com.atguigu.msmservice.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
@RequestMapping("edumsm/msm")
public class MsmController {

        @Autowired
        private MsmService msmService;
        @Autowired
        private RedisTemplate<String, String> redisTemplate;

        @GetMapping(value = "/send/{phone}")
        public R code(@PathVariable String phone) {
            //1===先从redis中获取验证码
            String code = redisTemplate.opsForValue().get(phone);
            if (!StringUtils.isEmpty(code)){
                return R.ok();
            }
            //2===如果redis取不到，再发送
            //设置5mins过期时间，放入redis缓存
            //通过该方法 发送短信验证码
            boolean isSend = msmService.send(phone);
            if(isSend) {
                return R.ok();
            } else {
                return R.error().message("发送短信失败");
            }
        }

}
